/*!
 *  @author  xiaody <xiaody09@163.com>
 *  @description  Basic module for BDB.
 */

// Basic functions.
function $(selector, context){
	'use strict';
	context = context || document;

	if(/^#([\w\u00c0-\uFFFF\-]+)$/.test(selector))
		return context.getElementById(RegExp.$1);
	else if(/^\.([\w\u00c0-\uFFFF\-]+)$/.test(selector))
		return context.getElementsByClassName(RegExp.$1)[0];

	return context.querySelector(selector);
}
function $$(selector, context){
	'use strict';
	context = context || document;

	if(/^\.([\w\u00c0-\uFFFF\-]+)$/.test(selector))
		return makeArray( context.getElementsByClassName(RegExp.$1) );

	return makeArray( context.querySelectorAll(selector) );
}

// Turn an array like object into array.
// Notice: this can't ensure that the result can pass Array.isArray() test.
function makeArray(list){
	return Array.prototype.slice.call(list, 0);
}

// Content scripts and webpage scripts are double-blind,
// so sometimes we have to inject a piece of script into DOM
// to use/change the variables in webpage scripts' context.
function inject(func){
    var script = document.createElement("script"), func = func.toString();
    document.documentElement.appendChild(script);
    script.textContent = "(" + func + ")();";
}

// Prevent page from loading some resouces by
// listening to the "beforeload" event in webkit.
// Notice: this won't work for requests from plugins.
function blockBySrc(arr){
	'use strict';

	if(!Array.isArray(arr))
		arr = [arr];
	document.addEventListener('beforeload', function(e){
		'use strict';
		var t = e.target, src = t.src, p = t.parentNode;
		if(!src) return;
		for(var s=src.toLowerCase(), i=0, l=arr.length; i<l; i++){
			if(s.match(arr[i])){
				e.preventDefault(); e.stopPropagation();
				p.removeChild(t);
				return;
			}
		}
	}, true);
}

// Prototype extend.
function extend(base, addition){
	'use strict';
	for(var key in addition)
		base[key] = addition[key];
	return base;
}
extend(HTMLElement.prototype, {
	attr : function(name, val){
		if(arguments.length === 2) {
			this.setAttribute(name, val);
		} else {
			if(typeof name === 'string'){
				return this.getAttribute(name);
			} else if(typeof name === 'object'){
				val = name;
				for(var key in val)
					this.setAttribute(key, val[key]);
			}
		}
		return this;
	},
	pushChild : function(child){
		if(child instanceof HTMLElement === true || child.nodeType === 3)
			this.appendChild(child);
		return this;
	},
	html : function(val){
		if(typeof val === 'undefined')
			return this.innerHTML;
		this.innerHTML = val;
		return this;
	},
	$text : function(val){
		if(typeof val === 'undefined')
			return this.textContent;
		this.textContent = val;
		return this;
	},
	offset : function(){
		var top = 0, left = 0, elem = this;
		do {
			top += elem.offsetTop;
			left += elem.offsetLeft;
		} while(elem = elem.offsetParent);

		return {
			'top' : top,
			'left' : left
		};
	}
});
extend(Array.prototype, {
	contains : function(val){
		return this.indexOf(val) > -1;
	}
});

// Ajax.
var Ajax = {
	get : function(url, callback){
		var XHR = new XMLHttpRequest();
		XHR.open("GET", url, true);
		XHR.onreadystatechange = function(){
			if( XHR.readyState === 4 && (XHR.status === 200 || XHR.status === 304) ){
				callback.success(XHR.responseText);
				XHR = null;
			} else if(callback.fail) {
				callback.fail(XHR.responseText);
				XHR = null;
			}
		}
		XHR.send(null);
	},
	post : function(url, dataObj, callback){
		var XHR = new XMLHttpRequest();
		var content = '';
		for(var key in dataObj) {
			content += key + '=' + dataObj[key] + '&';
		}
		XHR.open("POST", url, true);
		XHR.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
		XHR.onreadystatechange = function(){
			if( XHR.readyState === 4 && (XHR.status === 200 || XHR.status === 304) ){
				callback.success(XHR.responseText);
				XHR = null;
			} else if(callback.fail) {
				callback.fail(XHR.responseText);
				XHR = null;
			}
		}
		XHR.send(content);
	}
}

// Global variables.
const BDB_DEBUG = /E8E039/i.test(document.cookie),
	dummyFunction = function(){},
	log = BDB_DEBUG ? function(s){console.log(s)} : dummyFunction;
const pathhead = location.pathname.charAt(1),
	domainhead = top.location.hostname.split('.')[0];

//////////////////////////
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame;
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
